package com.iris.java8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author : Iris.Xiao
 * @date : 2018/01/17 16:10
 * @description :
 */
public class Car {
    public static Car create(Supplier< Car > supplier ) {
        return supplier.get();
    }

    public static void collide(Car car ) {
        System.out.println( "Collided " + car.toString() );
    }

    public void follow(Car another ) {
        System.out.println( "Following the " + another.toString() );
    }

    public void repair() {
        System.out.println( "Repaired " + this.toString() );
    }

    public static void main(String[] args) {
        Car car = Car.create( Car::new );
        Car car1 = Car.create( Car::new );
        List< Car > cars = Arrays.asList( car,car1 );
        System.out.println("============");
        cars.forEach( Car::collide );
        System.out.println("============");
        cars.forEach( Car::repair );
        System.out.println("============");
        Car police = Car.create( Car::new );
        cars.forEach( police::follow );

        //方法参数
        Consumer<Car> consumer = Car::collide;
        //方法执行accept
        cars.forEach(x -> consumer.accept(x));
    }
}
